package cliente.vista;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;

import cliente.controle.GerenteControlador;

public class CriaClienteView {

	private JFrame tela;
	private JPanel painel;
	private JLabel loginLabel;
	private JLabel nomeLabel;
	private JLabel senhaLabel;
	private JLabel senhaConfirmacaoLabel;
	private JLabel emailLabel;
	private JLabel cpfCnpjLabel;
	private JTextField nome;
	private JTextField login;
	private JTextField cpfCnpj;
	private JPasswordField senha;
	private JPasswordField senhaConfirmacao;
	private JTextField email;
	private JRadioButton pessoaFisica;
	private JRadioButton pessoaJuridica;
	private ButtonGroup grupo;
	private JButton criar;
	private JButton cancelar;
	private ActionListener ouvinte;

	public void iniciaCriaClienteView() {
		tela = new JFrame();
		ouvinte = new acao();

		nome = new JTextField();
		login = new JTextField();
		senha = new JPasswordField();
		senhaConfirmacao = new JPasswordField();
		email = new JTextField();
		cpfCnpj = new JTextField();

		criar = new JButton("Criar");
		criar.addActionListener(ouvinte);

		cancelar = new JButton("Cancelar");
		cancelar.addActionListener(ouvinte);

		loginLabel = new JLabel("Login");
		nomeLabel = new JLabel("Nome");
		cpfCnpjLabel = new JLabel("Cpf/Cnpj");
		senhaLabel = new JLabel("Senha");
		senhaConfirmacaoLabel = new JLabel("Confirmação da Senha");
		emailLabel = new JLabel("Email");

		pessoaFisica = new JRadioButton("Pessoa Fisica");
		pessoaJuridica = new JRadioButton("Pessoa Juridica");

		grupo = new ButtonGroup();
		grupo.add(pessoaFisica);
		grupo.add(pessoaJuridica);
		pessoaFisica.setSelected(true);

		tela.setBounds(500, 300, 400, 300);
		tela.setResizable(false);
		tela.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		tela.setTitle("Cria Cliente");

		painel = new JPanel();

		GroupLayout layout = new GroupLayout(painel);
		painel.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		organizaFields(layout);

		tela.setLayout(new BorderLayout());
		tela.add(painel, BorderLayout.CENTER);
		tela.setVisible(true);
	}

	private void criaGruposVerticais(GroupLayout layout) {
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(nomeLabel).addComponent(nome));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(loginLabel).addComponent(login));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(emailLabel).addComponent(email));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(pessoaFisica).addComponent(pessoaJuridica));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(cpfCnpjLabel).addComponent(cpfCnpj));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(senhaLabel).addComponent(senha));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(senhaConfirmacaoLabel).addComponent(
						senhaConfirmacao));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(criar).addComponent(cancelar));
		layout.setVerticalGroup(vGroup);
	}

	private void criaGruposHorizontais(GroupLayout layout) {
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(nomeLabel).addComponent(loginLabel).addComponent(
						emailLabel).addComponent(pessoaFisica).addComponent(
						cpfCnpjLabel).addComponent(senhaLabel).addComponent(
						senhaConfirmacaoLabel).addComponent(criar));
		hGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(nome).addComponent(login).addComponent(email)
				.addComponent(pessoaJuridica).addComponent(cpfCnpj)
				.addComponent(senha).addComponent(senhaConfirmacao)
				.addComponent(cancelar));
		layout.setHorizontalGroup(hGroup);
	}

	private void organizaFields(GroupLayout layout) {
		criaGruposHorizontais(layout);
		criaGruposVerticais(layout);
	}

	// Essa classe só passará os dados para a classe de controle adequada
	class acao implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == criar) {
				if (GerenteControlador.validaDadosCliente(nome.getText(), login
						.getText(), String.copyValueOf(senha.getPassword()),
						String.copyValueOf(senhaConfirmacao.getPassword()),
						email.getText(), cpfCnpj.getText())) {
					try {
						GerenteControlador.criaCliente(tela, nome.getText(),
								login.getText(), String.copyValueOf(senha
										.getPassword()), email.getText(),
								cpfCnpj.getText(), pessoaFisica.isSelected());
						tela.dispose();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}

				} else {
					JOptionPane.showMessageDialog(tela, "Dados Incompletos");
				}
			} else if (e.getSource() == cancelar) {
				tela.dispose();
			}
		}

	}
}
